﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge;
using AForge.Math.Geometry;
namespace AForge
{
    
    public partial class Form1 : Form
    {
        FilterInfoCollection VideoCaptureDevices;
        VideoCaptureDevice VideoSource;
        Bitmap image,im;
        float Href = 0;
        float Sref = 0;
        float H; // színérték
        float H2;
        float H3;
        float S;
        float S2;
        float S3;
        int x, y;
        int x_osszeg = 0;
        int x_db = 0;
        int x_atlag = 0;
        int y_osszeg = 0;
        int y_db = 0;
        int y_atlag = 0;
        DateTime ido1 = new DateTime();
        DateTime ido2 = new DateTime();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            VideoSource = new VideoCaptureDevice(VideoCaptureDevices[0].MonikerString);
            VideoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame);
            VideoSource.DesiredFrameRate = 1;
            VideoSource.DesiredFrameSize = new Size(160, 120);
            VideoSource.Start();
        }

        Bitmap b = new Bitmap(160, 120);

        void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            VideoSource.NewFrame -= VideoSource_NewFrame;
            
            image = (Bitmap)eventArgs.Frame.Clone();
            im=new Bitmap(160,120);
            
            for (x = 0; x < image.Width - 1; x++)
            {
                for (y = 0; y < image.Height - 1; y++)
                {
                    H = image.GetPixel(x, y).GetHue();
                    H2 = image.GetPixel(x + 1, y).GetHue();
                    H3 = image.GetPixel(x, y + 1).GetHue();

                    S = image.GetPixel(x, y).GetSaturation();
                    S2 = image.GetPixel(x + 1, y).GetSaturation();
                    S3 = image.GetPixel(x, y + 1).GetSaturation();
                    if (H > Href - 2 & H < Href + 2 & S > Sref - 0.1 &
                        H2 > Href - 2 & H2 < Href + 2 & S2 > Sref - 0.1 &
                        H3 > Href - 2 & H3 < Href + 2 & S3 > Sref - 0.1
                        )
                    {
                        im.SetPixel(x, y, Color.White);
                        //x_osszeg += x;
                        //x_db++;
                        //y_osszeg += y;
                        //y_db++;
                        
                    }
                    pictureBox1.Image = image;
                    
                    pictureBox2.Image = im;
                    /*else
                    {
                        kep2.SetPixel(x, y, Color.Black);
                    }*/
                }
            }

            VideoSource.NewFrame += VideoSource_NewFrame;
            
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (VideoSource.IsRunning)
                VideoSource.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
        }

        

        private void startcam_Tick(object sender, EventArgs e)
        {
            
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                Href = new Bitmap(pictureBox1.Image).GetPixel(e.X, e.Y).GetHue();
                Sref = new Bitmap(pictureBox1.Image).GetPixel(e.X, e.Y).GetSaturation();
                
            }
        }
    }
}
